<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
/* 
	上传
*/
class CI_Upload{
	
	// 临时文件
	private $tmp_file;
	
	// 根目录
	public $root_path;
	
	// 文件保存的目录、绝对路径
	public $save_dir;
	private $save_path;
	
	// 允许上传的类型 jpe|png|gif ...
	public $allow_mimes;
	
	// 文件大小最大值
	public $max_size;
	
	// 原始图片尺寸
	private $image_width;
	private $image_height;
	
	// 上传图片尺寸限定
	public $image_min_width;
	public $image_min_height;
	public $image_max_width;
	public $image_max_height;
	
	// 上传后文件的属性
	public $file_path;
	public $file_size;
	public $file_mime;
	public $file_name;
	
	public $error = array();
	
	public function __construct($option = array()){
		$this->init($option);
	}
	
	public function init($option){
		$this->root_path = isset($option['root_path']) ? $option['root_path'] : FCPATH;
		$this->save_dir = isset($option['save_dir']) ? rtrim($option['save_dir'], '/') : '/uploads';
		$this->allow_mimes = isset($option['allow_mimes']) ? $option['allow_mimes'] : '';
		$this->max_size = isset($option['max_size']) ? $option['max_size'] : 0;
		$this->image_min_width = isset($option['image_min_width']) ? $option['image_min_width'] : 0;
		$this->image_min_height = isset($option['image_min_height']) ? $option['image_min_height'] : 0;
		$this->image_max_width = isset($option['image_max_width']) ? $option['image_max_width'] : 0;
		$this->image_max_height = isset($option['image_max_height']) ? $option['image_max_height'] : 0;
	}
	
	// 批量上传
	public function multiple_save($field){
		if(!isset($_FILES[$field])){
			return $this->_error('未选择上传文件');
		}
		$files = $_FILES[$field];
		$return = array();
		$file_ary = $this->array_files($files);
		foreach($file_ary as $_file){
			$save = $this->save($_file);
			if(!$save['errcode']){
				$return[] = $save;
			}
		}
		return $return;
	}
	
	// 单个文件上传
	public function save($_file){
		if(isset($_FILES[$_file])){
			$_file = $_FILES[$_file];
		}
		
		if(!$_file){
			return $this->_error('未选择上传文件');
		}
		
		if(!is_uploaded_file($_file['tmp_name']) || $_file['error']){
			return $this->_error('文件错误');
		}
		
		if(!$this->check_save_path()){
			return $this->_error('目录创建失败');
		}
		
		$this->file_mime = $this->get_file_mime($_file['name']);
		if(!$this->check_file_mime()){
			return $this->_error('文件类型不正确');
		}
		
		$this->file_size = $_file['size'];
		if(!$this->check_file_size()){
			return $this->_error('文件大小超过限制');
		}
		
		$this->tmp_file = $_file['tmp_name'];
		if(!$this->check_image_size()){
			return $this->_error('图片尺寸不正确');
		}
		
		$this->set_file_name();
		$save_file = $this->save_path.'/'.$this->file_name;
		if(!move_uploaded_file($this->tmp_file, $save_file)){
			return $this->_error();
		}
		
		$is_image = $this->is_image();
		$return = array(
			'file_name' => $this->file_name,
			'file_size' => $this->file_size,
			'file_type' => $_file['type'],
			'file_mime' => $this->file_mime,
			'file_path' => $this->file_path.'/'.$this->file_name,
			'is_image' => $this->is_image()
		);
		
		if($is_image){
			$return['image_width'] = $this->image_width;
			$return['image_height'] = $this->image_height;
		}
		
		return $return;
	}
	
	public function array_files($arr){
		$file_ary = array();
		$file_count = count($arr['name']);
		if($file_count < 1){
			return $this->_error('格式错误');
		}
		$file_keys = array_keys($arr);
		for($i=0; $i<$file_count; $i++){
			foreach($file_keys as $key){
				$file_ary[$i][$key] = $arr[$key][$i];
			}
		}
		return $file_ary;
	}
	
	private function get_file_mime($str){
		return strtolower(substr(strrchr($str, '.'), 1));
	}
	
	private function check_file_size(){
		if($this->max_size > 0 && $this->file_size > $this->max_size * 1024){
			return false;
		}
		return true;
	}
	
	private function check_file_mime(){
		if(!$this->allow_mimes){
			return true;
		}
		$mimes = explode('|', $this->allow_mimes);
		if(!in_array($this->file_mime, $mimes)){
			return false;
		}
		return true;
	}
	
	private function check_save_path(){
		$file_path = $this->save_dir.'/'.date('Ymd');
		$save_path = rtrim($this->root_path, '/').$file_path;
		if(!is_dir($save_path)){
			if(!mkdir($save_path, 0775, true)){
				return false;
			}
		}
		$this->file_path = $file_path;
		$this->save_path = $save_path;
		return true;
	}
	
	private function check_image_size(){
		if(!$this->is_image()){
			return true;
		}
		$res = getimagesize($this->tmp_file);
		if(!$res){
			return false;
		}
		$width = $res[0];
		$height = $res[1];
		$this->image_width = $width;
		$this->image_height = $height;
		if($this->image_min_width > 0 && $width < $this->image_min_width){
			return false;
		}
		if($this->image_min_height > 0 && $height < $this->image_min_height){
			return false;
		}
		if($this->image_max_width > 0 && $width > $this->image_max_width){
			return false;
		}
		if($this->image_max_height > 0 && $height > $this->image_max_height){
			return false;
		}
		return true;
	}
	
	private function is_image(){
		$mimes = array('jpg', 'jpeg', 'gif', 'png');
		return in_array($this->file_mime, $mimes) ? 1 : 0;
	}
	
	private function set_file_name(){
		$this->file_name = date('His').substr(microtime(), 2, 5).sprintf('%02d', rand(0, 99));
		$this->file_name = $this->file_name.'.'.$this->file_mime;
	}
	
	private function _error($msg = '上传失败', $code = -1){
		$this->error = array(
			'errcode' => $code,
			'errmsg' => $msg
		);
		return $this->error;
	}
	
}